package com.wangsd.web.mqpay.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.mqpay.entity.Customer;
import com.wangsd.web.mqpay.service.CustomerService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "客户(商圈)管理")
@RestController
@RequestMapping("/mqpay/customer")
@Slf4j
public class CustomerController extends MyController {
    @Resource
    private CustomerService service;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             Customer queryObj) {
        log.debug("分页查询列表-controller");
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Customer> list = service.selectPage(queryObj, loginUser.getCode());
        return Result.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "新增或者修改")
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody Customer entity) {
        log.debug("新增或者修改-controller");
        service.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "删除客户(商圈)")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "客户(商圈)id", required = true) @PathVariable Long id) {
        log.debug("删除客户(商圈)-controller");
        service.delete(id);
        return Result.success();
    }

    @ApiOperation(value = "查询所有客户（商圈）（下拉列表）")
    @PostMapping("/selectAllCustomer")
    public Result selectAllCustomer(@ApiParam(value = "类型") @RequestParam(value = "code", required = false) String code) {
        log.debug("查询所有客户（商圈）（下拉列表）-controller");
        SysUser loginUser = getUserInfo();
        List<Customer> list = service.selectAllCustomer(loginUser.getCode(), code);
        return Result.success(list);
    }

    @ApiOperation(value = "刷新api秘钥")
    @GetMapping("/{id}/refreshApi")
    public Result refreshApi(@ApiParam(value = "客户(商圈)id", required = true) @PathVariable Long id) {
        log.debug("刷新api秘钥-controller");
        Map map = service.refreshApi(id);
        return Result.success(map);
    }

    @ApiOperation(value = "获取可用金额")
    @GetMapping("/{id}/getUseableAmount")
    public Result getUseableAmount(@ApiParam(value = "客户(商圈)id", required = true) @PathVariable Long id) {
        log.debug("刷新api秘钥-controller");
        Customer customer = service.selectById(id);
        Map map = new HashMap();
        map.put("useableAmount", customer.getUseableAmount() * 0.01);
        map.put("settleAmount", customer.getUseableAmount() * 0.01);
        return Result.success(map);
    }

    @ApiOperation(value = "申请提现")
    @PostMapping("/createCustomerFlow")
    public Result createCustomerFlow(@ApiParam(value = "客户(商圈)id", required = true) @RequestParam Long id,
                                     @ApiParam(value = "提现金额", required = true) @RequestParam Integer settleAmount) {
        log.debug("申请提现-controller");
        int amount = settleAmount * 100;
        service.createCustomerFlow(id, amount);
        return Result.success();
    }
}
